package ch15;

import javafoundations.LinearNode;

import java.util.LinkedList;

/**
 * Created by zhangyunqi on 2017/10/15.
 */

public class LinkedQueue<T> implements Queue <T> {
    private int count;
    private LinearNode <T> front, rear;
    private LinkedList list;

    public LinkedQueue() {
        count = 0;
        front = rear = null;
    }

    public void enqueue(T element) {
        LinearNode <T> node = new LinearNode <T> ( element );
        if (isEmpty ())
            front = node;
        else
            rear.setNext ( node );
        rear = node;
        count++;
    }

    public T dequeue() throws EmptyCollectionException {
        if (count == 0)
            throw new EmptyCollectionException ( "queue" );
        T t = front.getElement ();
        front = front.getNext ();
        count--;
        return t;
    }

    public T first() throws EmptyCollectionException {
        if (count == 0)
            throw new EmptyCollectionException ( "queue" );
            return front.getElement ();
    }


    public void add(Object obj) {
        this.list.add ( obj );
    }

    public Object get() {
        return this.list.getFirst ();
    }

    public void next() {
        this.list.removeFirst ();
    }

    public boolean isEmpty() {
        if (count == 0) {
            return true;
        } else
            return false;

    }

    public int size() {
        return count;
    }


    public String toString() {
        String result = "";
        LinearNode <T> current = front;
        while (current != null) {
            result = result + (current.getElement ()).toString () + "\n";
            current = current.getNext ();
        }
        return result;
    }

    }

